layui.use(['element','form','upload'], function() {
    var $ = layui.jquery
        ,form = layui.form
        ,upload = layui.upload
        , element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
    form.render();
    getUserInfo();
    //自定义验证规则
    form.verify({
        password: [
            /^[\S]{6,20}$/
            ,'密码必须6到20位，且不能出现空格'
        ],
        password2:function(value){
            console.log(value)
            if(value != $("#newPassword").val()){
                return '请确保新密码与确认密码一致'
            }
            if(value == $("#curretPassword").val()){
                return '新密码不能与老密码一致'
            }
        }
    });
    //触发事件
    var active = {
        tabChange: function(){
            //切换到指定Tab项
            element.tabChange('demo', '22'); //切换到：用户管理
        }
    };
    $('.site-demo-active').on('click', function(){
        var othis = $(this), type = othis.data('type');
        active[type] ? active[type].call(this, othis) : '';
    });
    $(".layui-tab-content").height($(window).height()-100);

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        ,url: getServerPath("UPLOADAVATAR") //改成您自己的上传接口
        ,headers: { SYSTOKEN: getSettings('token') }
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#avatarUrl').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code > 0){
                return layer.msg('上传失败');
            }
            parent.changeAvatarUrl(res.data)
            //上传成功
        }
        ,error: function(){
            return layer.msg('上传失败,请重新上传');
            // //演示失败状态，并实现重传
            // var demoText = $('#demoText');
            // demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            // demoText.find('.demo-reload').on('click', function(){
            //     uploadInst.upload();
            // });
        }
    });

    //个人信息提交
    form.on('submit(btn_save)', function(data){
        data = data.field;
        var serverpath = getServerPath("UPDATECURRENTINFO");
        ajaxSetting({
            url:serverpath,
            data:JSON.stringify(data),
            success:function(res){
                layer.msg("保存成功",{icon:6})
                parent.changeCurrentInfo(data.nickName);
            },
            error:function(res){
                console.log(res.field)
            }
        })
        return false;
    });

    //修改密码
    form.on("submit(btn_submit)",function (data) {
        data = data.field;
        ajaxSetting({
            url:getServerPath("UPDATEPASSWORD"),
            data:JSON.stringify(data),
            success:function(res){
                layer.msg("密码修改成功,需要重新登录系统",{icon:6})
                setTimeout(function(){
                    parent.window.location.href = "/page/system/login.html";
                },1000)
            }
        })
        return false;
    })
    //获取个人信息
    function getUserInfo() {
        ajaxSetting({
            url: getServerPath("GETUSERINFO"),
            type: 'get',
            success: function(res) {
                form.val('userinfo', res.data);
                $('#avatarUrl').attr('src', res.data.avatarUrl); //图片链接（base64）
            }
        })
    }
});

